﻿using CSharpDay15__Auth_.Adapters.Adapters;
using CSharpDay15__Auth_.Adapters.Interfaces;
using CSharpDay15__Auth_.Data.Model;
using CSharpDay15__Auth_.Models;
using Microsoft.AspNet.Identity; //for User.Identity.GetUserId();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CSharpDay15__Auth_.Controllers
{
    public class HomeController : Controller
    {
        private IToDoAdapter _adapter;

        public HomeController()
        {
            _adapter = new ToDoAdapter();
        }

        public HomeController(IToDoAdapter a)
        {
            _adapter = a;
        }

        public ActionResult Index()
        {
            if (User.IsInRole(Roles.Admin)) ViewBag.Role = "Admin";
            else if (User.IsInRole(Roles.User)) ViewBag.Role = "User";
            else ViewBag.Role = "";
            return View();
        }

        //only allow people to access this Action IF they are logged in
        [Authorize]
        public ActionResult ToDoIndex()
        {
            string userId = User.Identity.GetUserId(); //WE HAVE TO HAVE... "using Microsoft.AspNet.Identity" at the top
            List<ToDo> model = _adapter.GetToDos(userId);
            return View(model);
        }

        //Only let the user enter this action if they are logged in AND an admin
        [Authorize(Roles = Roles.Admin)]
        public ActionResult AdminHome()
        {
            List<UserViewModel> model = _adapter.GetAllUsers();
            return View(model);
        }

        [Authorize(Roles = Roles.Admin)]
        public ActionResult ToggleDisabledUser(string id)
        {
            _adapter.ToggleIsDisabled(id);
            return RedirectToAction("AdminHome");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}